//
//  SelectButtonView.m
//  CZT_HN_Longrise
//
//  Created by 程虎 on 2017/5/9.
//  Copyright © 2017年 OSch. All rights reserved.
//

#define KDeviceWidth [UIScreen mainScreen].bounds.size.width
#define KDeviceHeight [UIScreen mainScreen].bounds.size.height
#define ViewWidth 300
#define MessageX 24.5
#define MessageWidth ViewWidth - (MessageX * 2)

#import "LRAlertView.h"
#import <SDAutoLayout.h>

@interface LRAlertView()
{
    CGFloat viewHeight;
}
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *messageLab;
@property (nonatomic, strong) UIView *showView;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *otherBtn;
@end

@implementation LRAlertView

- (instancetype)initWithCustomView:(UIView *)view dismissWhenTouchedBackground:(BOOL)isDismiss
{
    self = [super init];
    if (self) {
        self.backgroundColor = RGBA(0, 0, 0, 0.7);
        self.frame = CGRectMake(0, 0, KDeviceWidth, KDeviceHeight);
        
        [self config_ShowView:isDismiss];
        [self setCustomView:view];
    }
    return self;
}

- (instancetype)initWithbuttonTitle:(NSString *)titles Message:(NSString *)message CancelButtonTitle:(NSString *)cancelTitles OtherButtonsTitle:(NSString *)otherTitles
{
    self = [super init];
    if (self) {
        self.backgroundColor = RGBA(0, 0, 0, 0.7);
        self.frame = CGRectMake(0, 0, KDeviceWidth, KDeviceHeight);
        
        [self config_ShowView:NO];
        self.showView.backgroundColor = [UIColor whiteColor];
        [self set_UITitle:titles Message:message CancelButtonTitle:cancelTitles OtherButtonsTitle:otherTitles normalType:NO];
    }
    return self;
}

- (instancetype)initWithAlertTitle:(NSString *)titles Message:(NSString *)message ButtonTitle:(NSString *)cancelTitles OtherTitle:(NSString *)otherTitles
{
    self = [super init];
    if (self) {
        self.backgroundColor = RGBA(0, 0, 0, 0.7);
        self.frame = CGRectMake(0, 0, KDeviceWidth, KDeviceHeight);
        
        [self config_ShowView:NO];
        self.showView.backgroundColor = [UIColor whiteColor];
        [self set_UITitle:titles Message:message CancelButtonTitle:cancelTitles OtherButtonsTitle:otherTitles normalType:YES];
    }
    return self;
}


- (void)config_ShowView:(BOOL) isdismiss
{
    if (isdismiss) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
        [tap addTarget:self action:@selector(removeView)];
        [self addGestureRecognizer:tap];
    }
    
    self.showView = [[UIView alloc] init];
    self.showView.backgroundColor = [UIColor clearColor];
    self.showView.center = self.center;
    [self addSubview:self.showView];
    LRViewRadius(self.showView, 5, 1, [UIColor clearColor]);
}



- (void)setCustomView:(UIView *)customView
{
    [self.showView addSubview:customView];
    
    self.showView.sd_layout
    .centerXEqualToView(self)
    .centerYEqualToView(self)
    .widthIs(customView.frame.size.width)
    .heightIs(customView.frame.size.height);
    
    customView.sd_layout.spaceToSuperView(UIEdgeInsetsZero);
    
    [self.showView setupAutoHeightWithBottomView:customView bottomMargin:0];
}

- (void)set_UITitle:(NSString *)titles Message:(NSString *)message CancelButtonTitle:(NSString *)cancelTitles OtherButtonsTitle:(NSString *)otherTitles normalType:(BOOL ) isNormal
{
    if (titles.length > 0) {
        self.titleLab = [[UILabel alloc] init];
        self.titleLab.font = [UIFont systemFontOfSize:18];
        self.titleLab.textColor = KButtonColor;
        self.titleLab.textAlignment= NSTextAlignmentCenter;
        self.titleLab.text = titles;
        [self.showView addSubview:self.titleLab];
    }
    
    self.messageLab = [[UILabel alloc] init];
    self.messageLab.font = [UIFont systemFontOfSize:15];
    self.messageLab.textColor = [Util colorWithHexString:@"#333333"];
    self.messageLab.text = message;
    self.messageLab.numberOfLines = 0;
    [self.showView addSubview:self.messageLab];
    
    self.otherBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.otherBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [self.otherBtn setTitle:otherTitles forState:UIControlStateNormal];
    [self.otherBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.otherBtn setBackgroundColor:[Util colorWithHexString:@"#098edd"]];
    [self.showView addSubview:self.otherBtn];
    
    [self.otherBtn addTarget:self action:@selector(buttonsClick:) forControlEvents:UIControlEventTouchUpInside];
    
    if (cancelTitles.length > 0) {
        self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.cancelBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [self.cancelBtn setTitle:cancelTitles forState:UIControlStateNormal];
        [self.cancelBtn setTitleColor:KButtonColor forState:UIControlStateNormal];
        [self.cancelBtn setBackgroundColor:[Util colorWithHexString:@"#e6f3fc"]];
        [self.showView addSubview:self.cancelBtn];
        [self.cancelBtn addTarget:self action:@selector(buttonsClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
    CGFloat top = 17;
    CGFloat messageX = MessageX;
    
    self.showView.sd_layout
    .centerXEqualToView(self)
    .centerYEqualToView(self)
    .widthIs(ViewWidth);
    
    if (titles.length > 0) {
        self.titleLab.sd_layout
        .leftSpaceToView(self.showView, 0)
        .topSpaceToView(self.showView, top)
        .rightSpaceToView(self.showView, 0)
        .heightIs(17);
        
        self.messageLab.sd_layout
        .leftSpaceToView(self.showView, messageX)
        .topSpaceToView(self.titleLab, top)
        .rightSpaceToView(self.showView, messageX)
        .autoHeightRatio(0);
    }
    else {
        self.messageLab.sd_layout
        .leftSpaceToView(self.showView, messageX)
        .topSpaceToView(self.showView, top)
        .rightSpaceToView(self.showView, messageX)
        .autoHeightRatio(0);
    }
    
    CGFloat btnY =  21.5;
    CGFloat btnH = 44;
    
    if (isNormal) {
     
        self.titleLab.textColor = KButtonColor;
        self.messageLab.textAlignment = NSTextAlignmentCenter;
        self.otherBtn.backgroundColor = [UIColor whiteColor];
        self.cancelBtn.backgroundColor = [UIColor whiteColor];
        
        UIImageView *line = [UIImageView new];
        line.backgroundColor = K_LineColor;
        UIImageView *line1 = [UIImageView new];
        line1.backgroundColor = K_LineColor;
        
        [self.showView sd_addSubviews:@[line,line1]];
        
        line.sd_layout.leftEqualToView(self.showView)
        .topSpaceToView(self.messageLab, btnY)
        .rightEqualToView(self.showView)
        .heightIs(0.5);
        
        if (cancelTitles.length > 0 && otherTitles.length > 0) {
            self.cancelBtn.tag = 0;//在左
            self.otherBtn.tag = 1; //在右
            [self.otherBtn setTitleColor:KButtonColor forState:UIControlStateNormal];
            [self.cancelBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            
            self.cancelBtn.sd_layout
            .leftEqualToView(line)
            .topSpaceToView(line, 0)
            .widthIs(ViewWidth * 0.5 - 0.25)
            .heightIs(btnH);
            
            line1.sd_layout
            .leftSpaceToView(self.cancelBtn, 0)
            .topEqualToView(self.cancelBtn)
            .widthIs(0.5)
            .heightRatioToView(self.cancelBtn, 1);
            
            self.otherBtn.sd_layout
            .leftEqualToView(line1)
            .topEqualToView(line1)
            .rightEqualToView(self.showView)
            .heightIs(btnH);
            
            [self.showView setupAutoHeightWithBottomView:self.otherBtn bottomMargin:0];
        }
        else if (cancelTitles.length > 0 && !otherTitles.length) {
            self.cancelBtn.tag = 0;//在左
            [self.cancelBtn setTitleColor:KButtonColor forState:UIControlStateNormal];
            
            self.cancelBtn.sd_layout
            .leftEqualToView(line)
            .topSpaceToView(line, 0)
            .rightEqualToView(line)
            .heightIs(btnH);
            [self.showView setupAutoHeightWithBottomView:self.cancelBtn bottomMargin:0];
        }
        else {
            self.otherBtn.tag = 0; //在右
            [self.otherBtn setTitleColor:KButtonColor forState:UIControlStateNormal];
            
            self.otherBtn.sd_layout
            .leftEqualToView(line)
            .topSpaceToView(line, 0)
            .rightEqualToView(line)
            .heightIs(btnH);
            [self.showView setupAutoHeightWithBottomView:self.otherBtn bottomMargin:0];
        }
    }
    else {
        
        LRViewRadius(self.otherBtn, 7.5, 0.5, [UIColor clearColor]);
        LRViewRadius(self.cancelBtn, 7.5, 0.5, [UIColor clearColor]);
        
        self.otherBtn.sd_layout
        .leftEqualToView(self.messageLab)
        .topSpaceToView(self.messageLab, btnY)
        .rightEqualToView(self.messageLab)
        .heightIs(btnH);
        
        if (cancelTitles.length > 0) {
            
            self.otherBtn.tag = 0; //在上
            self.cancelBtn.tag = 1;//在下
            
            self.cancelBtn.sd_layout
            .leftEqualToView(self.otherBtn)
            .topSpaceToView(self.otherBtn, 15)
            .rightEqualToView(self.otherBtn)
            .heightIs(btnH);
            
            [self.showView setupAutoHeightWithBottomView:self.cancelBtn bottomMargin:24];
        }
        else {
            self.otherBtn.tag = 0; //只有一个
            [self.showView setupAutoHeightWithBottomView:self.otherBtn bottomMargin:24];
        }
    }
    
}


- (void)buttonsClick:(UIButton *)buttons
{
    if (self.btnClickBlock) {
        self.btnClickBlock(buttons.tag);
        [self removeView];
    }
}

- (void)removeView
{
    if (self) {
        [self removeFromSuperview];
    }
}

/**
 设置title字体大小
 @param titleFont 字号
 */
- (void)setTitleFont:(UIFont *)titleFont
{
    _titleFont = titleFont;
    self.titleLab.font = _titleFont;
}


/**
 设置内容字体大小

 @param messageFont 字号
 */
-(void)setMessageFont:(UIFont *)messageFont
{
    _messageFont = messageFont;
    self.messageLab.font = _messageFont;
}


/**
 设置title文字颜色

 @param titleColor 颜色
 */
- (void)setTitleColor:(UIColor *)titleColor
{
    _titleColor = titleColor;
    self.titleLab.textColor = _titleColor;
}


/**
 设置内容字体颜色

 @param messageColor 颜色
 */
- (void)setMessageColor:(UIColor *)messageColor
{
    _messageColor = messageColor;
    self.messageLab.textColor = _messageColor;
}


/**
 按钮文字数组

 @param btnTitlesAry 文字
 */
- (void)setBtnTitlesAry:(NSArray *)btnTitlesAry
{
    _btnTitlesAry = btnTitlesAry;
    [self setbuttons:_btnTitlesAry type:0];
}


/**
 按钮文字颜色数组

 @param btnColorAry    颜色
 */
- (void)setBtnColorAry:(NSArray *)btnColorAry
{
    _btnColorAry = btnColorAry;
    [self setbuttons:_btnColorAry type:1];
}


/**
 按钮背景颜色数组

 @param btnBackGroundColorAry 颜色
 */
- (void)setBtnBackGroundColorAry:(NSArray *)btnBackGroundColorAry
{
    _btnBackGroundColorAry= btnBackGroundColorAry;
}


/**
 创建按钮

 @param array 数组
 @param type 0 文字数组 1 颜色数组
 */
- (void)setbuttons:(NSArray *)array type:(NSInteger )type
{
    BOOL isHaveBtn = NO;
    for (id views in self.showView.subviews) {
        if ([views isKindOfClass:[UIButton class]]) {
            isHaveBtn = YES;
        }
    }
    
    if (isHaveBtn) {
        
        for (int i = 0; i < array.count; i++) {
            UIButton *btn = [self.showView viewWithTag:i];
            
            if (type == 0) {
                [btn setTitle:array[i] forState:UIControlStateNormal];
            }
            else if (type == 1) {
//                UIColor *colors = array[i];
                [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            }
            else {
                [btn setBackgroundColor:array[i]];
            }
        }
    }
    else {
        CGFloat btnX = 0;
        CGFloat btnY = viewHeight + 21.5;
        CGFloat btnW = MessageWidth;
        CGFloat btnH = 44;
        
        for (int i = 0; i < array.count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn.titleLabel setFont:[UIFont systemFontOfSize:16]];
            btn.tag = i;
            btn.frame = CGRectMake(btnX, btnY + i * btnH + 15, btnW, btnH);
            [self.showView addSubview:btn];
            
            LRViewRadius(btn, 7.5, 0.5, [UIColor clearColor]);
            
            if (type == 0) {
                [btn setTitle:array[i] forState:UIControlStateNormal];
            }
            else if (type == 1){
                [btn setTitleColor:array[i] forState:UIControlStateNormal];
            }
            else {
                [btn setBackgroundColor:array[i]];
            }
            
        }
        
        UIButton *btns = [self.showView viewWithTag:array.count - 1];
        self.showView.frame = CGRectMake(0, 0, ViewWidth, CGRectGetMaxY(btns.frame) + 21.5);
    }


}

#pragma mark - 计算label的高度
- (CGFloat )getlabsHeight:(NSString *)strings Font:(CGFloat)font Width:(CGFloat)width
{
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:font];
    
    return [strings boundingRectWithSize:CGSizeMake( width,MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.height;
}

- (void)showAlert{
    UIView *view = nil;
    for (UIWindow *window in [UIApplication sharedApplication].windows) {
        if (!window.isKeyWindow)continue;
        view = window;
        break;
    }
    [view addSubview:self];
}
@end
